﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using School.Admission;
using School.Organization;
using System.Data.Entity;


namespace School.Pages
{
    public partial class LoginPage : System.Web.UI.Page
    {
        SchoolEntities db = new SchoolEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            Session["UserId"] = "";
            Session["UserName"] = "";
            Session["SesuirtyArray"] = null;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ddlLoginType.SelectedIndex == 0)
            {
                ORG_Stuff c = Staff.GetByUserNamePassword(txtName.Text, txtPassword.Text);
                if (c != null)
                {
                    Int32 x = Convert.ToInt32(c.Id);
                    var privlage = fn_privilage.getpagecode(x);

                    lblMessage.Visible = false;
                    Session["UserId"] = c.Id.ToString();
                    Session["UserName"] = c.FName + " " + c.MName + " " + c.LName;
                    Session["privilage"] = (IQueryable<int>)privlage;
                    Response.Redirect("MainPage/MainPage.aspx");
                }
                else
                {
                    lblMessage.Text = "اسم المستخدم او كلمة السر غير صحيح";
                    lblMessage.Visible = true;
                    txtName.Focus();
                }
            }
            else if(ddlLoginType.SelectedIndex == 1)
            {
                School.Admission.Student st = new Student();
                TE_Student s = st.GetByUserNamePassword(txtName.Text, txtPassword.Text);
                if (s != null)
                {
                    Int32 x = Convert.ToInt32(s.Id);
                    var privlage = fn_privilage.getpagecode(x);

                    lblMessage.Visible = false;
                    Session["UserId"] = s.Id.ToString();
                    Session["UserName"] = s.Ar_FirstName + " " + s.Ar_SecondName + " " + s.Ar_ThirdNmae;
                    Session["privilage"] = (IQueryable<int>)privlage;
                    Session["studentId"] = s.Id.ToString();
                    Response.Redirect("MainPage/StudentFatherPage.aspx?TypeId=1");
                }
                else
                {
                    lblMessage.Text = "اسم المستخدم او كلمة السر غير صحيح";
                    lblMessage.Visible = true;
                    txtName.Focus();
                }
            }
            else if (ddlLoginType.SelectedIndex == 2)
            {

                SD_Father f = Father.GetByUserNamePassword(txtName.Text, txtPassword.Text);
                if (f != null)
                {
                    Int32 x = Convert.ToInt32(f.Id);
                    var privlage = fn_privilage.getpagecode(x);

                    lblMessage.Visible = false;
                    Session["UserId"] = f.Id.ToString();
                    Session["UserName"] = f.Ar_FirstName + " " + f.Ar_SecondName + " " + f.Ar_ThirdName;
                    Session["privilage"] = (IQueryable<int>)privlage;
                    Session["fatherId"] = f.Id.ToString();
                    db.Connection.Close();
                    Response.Redirect("MainPage/StudentFatherPage.aspx?");
                }
                else
                {
                    lblMessage.Text = "اسم المستخدم او كلمة السر غير صحيح";
                    lblMessage.Visible = true;
                    txtName.Focus();
                }
            }
        }
    }


}
